package com.scs.application.modules.fsd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.PinYinUtils;
import com.scs.application.modules.fsd.entity.FsdGroup;
import com.scs.application.modules.fsd.mapper.GroupMapper;
import com.scs.application.modules.fsd.service.GroupService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 */
@Service
public class GroupServiceImpl extends BaseServiceImpl<GroupMapper, FsdGroup> implements GroupService {

    @Override
    public Boolean saveGroup(String groupName, String remark) {
        FsdGroup group = new FsdGroup();
        group.setGroupName(groupName);
        group.setGroupNamePy(PinYinUtils.getAlpha(groupName));
        group.setRemark(remark);
        save(group);
        return true;
    }

    @Override
    public Map<String, Object> getGroups(String keyword, Integer currentPage, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", this.count(Wrappers.<FsdGroup>query().like("group_name", keyword).or().like("group_name_py", keyword).or().like("remark", keyword)));
        map.put("rows", this.list(
                Wrappers.<FsdGroup>query().like("group_name", keyword).or().like("group_name_py", keyword).or().like("remark", keyword).last("limit " + (currentPage - 1) * pageSize + "," + pageSize).orderByDesc("id")
        ));
        return map;
    }

    @Override
    public Boolean editGroupById(String groupName, String remark, String id) {
        FsdGroup group = this.getOne(Wrappers.<FsdGroup>query()
                .eq("id", id));
        group.setGroupName(groupName);
        group.setGroupNamePy(PinYinUtils.getAlpha(groupName));
        group.setRemark(remark);
        this.updateById(group);
        return true;
    }
}
